<div class="content-wrapper">
    <?php
    $form = $this->form;
    $form->prepare();

    ?>
    <?php echo $this->form()->openTag($form); ?>
    <section class="content-header">
        <?php echo $this->partial('layout/breadcrumb'); ?>

        <h1 class="pull-right" id="dbshop-menu-top">
            <a href="<?php echo $this->url('sales', ['action' => 'detail', 'id' => $this->orderInfo->getOrderId()], ['query' => $this->queryData]);?>" class="btn btn-default btn-sm"><i class="fa fa-arrow-left"></i> <?php echo $this->translate('返回订单详情'); ?></a>
            <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-check"></i> <?php echo $this->translate('保存订单金额调整'); ?></button>
            <?php echo $this->HelpUrl('changeOrderAmount'); ?>
        </h1>
    </section>

    <section class="content">

        <div class="row">
            <div class="col-md-12">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title"><?php echo $this->translate('订单金额调整'); ?></h3>
                    </div>

                    <div class="box-body">
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('订单金额'); ?></label>
                            <div class="col-sm-5">
                                <p class="form-control-static"><?php echo $this->orderInfo->getCurrencySymbol() . '&nbsp;' . $this->orderInfo->getOrderAmount(); ?></p>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('调整类型'); ?></label>
                            <div class="col-sm-2">
                                <?php echo $this->formElement($form->get('orderChangeType')); ?>
                                <?php echo $this->formElementErrors($form->get('orderChangeType'), ['class'=>'error-message']); ?>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('调整金额'); ?></label>
                            <div class="col-sm-3">
                                <div class="input-group">
                                    <span class="input-group-addon"><?php echo $this->orderInfo->getCurrencySymbol(); ?></span>
                                    <?php echo $this->formElement($form->get('orderChangeNum')); ?>
                                </div>
                                <?php echo $this->formElementErrors($form->get('orderChangeNum'), ['class'=>'error-message']); ?>
                            </div>

                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('调整后金额'); ?></label>
                            <div class="col-sm-3">
                                <div class="input-group">
                                    <span class="input-group-addon"><?php echo $this->orderInfo->getCurrencySymbol(); ?></span>
                                    <input type="text" name="finishAmount" id="finishAmount" value="<?php echo $this->orderInfo->getOrderAmount(); ?>" disabled class="form-control">
                                </div>
                            </div>
                            <div class="col-sm-2">
                                <button type="button" onclick="calculateOrderAmount();" class="btn btn-info"><i class="fa fa-calculator" aria-hidden="true"></i> <?php echo $this->translate('点击计算'); ?></button>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label"><?php echo $this->translate('操作备注'); ?><span class="must_add_value">*</span></label>
                            <div class="col-sm-6">
                                <?php echo $this->formElement($form->get('orderChangeInfo')); ?>
                                <?php echo $this->formElementErrors($form->get('orderChangeInfo'), ['class'=>'error-message']); ?>
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>

    </section>
    <?php echo $this->form()->closeTag(); ?>

</div>
<script type="text/javascript">
    $(document).ready(function() {
        $("#<?php echo $form->getName(); ?>").validate({
            rules: {
                orderChangeNum: {
                    required: true,
                    number: true,
                    min: 1
                },
                orderChangeInfo: {
                    required: true
                }
            },
            messages: {
                orderChangeNum: {
                    required: "<?php echo $this->translate('修改金额不能为空!'); ?>",
                    number: "<?php echo $this->translate('金额必须为整数!'); ?>",
                    min:"<?php echo $this->translate('金额最小为1'); ?>"
                },
                orderChangeInfo: {
                    required: "<?php echo $this->translate('备注不能为空!'); ?>"
                }
            }
        });
    });

    /**
     * 订单金额计算
     */
    function calculateOrderAmount() {
        let orderTotal  = <?php echo $this->orderInfo->getOrderAmount(); ?>;
        let editType    = $('#orderChangeType').val();
        let orderEditAmount = $('#orderChangeNum').val();

        if(orderEditAmount != '') {
            if(editType == '+') {
                orderTotal = accAdd(orderTotal, parseFloat(orderEditAmount));
            }
            if(editType == '-') {
                orderTotal = Subtr(orderTotal, parseFloat(orderEditAmount));
            }
        }

        $('#finishAmount').val(orderTotal);
    }
</script>
